package com.deliveryservicesystem.app.util;

import java.awt.Component;
import java.awt.Dimension;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
import javax.swing.JOptionPane;

/**
 * File Name: AppUtils.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.app.util
 * Author: Lance
 * Date: Jun 24, 2012
 * Editor:
 */
public class AppUtils
{

    /**
     * Locates the given component on the screen's center.
     *
     * @param component the component to be centered
     */
    public static void locateOnScreenCenter(Component component)
    {
        Dimension paneSize = component.getSize();
        Dimension screenSize = component.getToolkit().getScreenSize();
        component.setLocation((screenSize.width - paneSize.width) / 2,
                (screenSize.height - paneSize.height) / 2);
    }

    /**
     *
     * @param parentCopmonent
     * @param question
     * @param title
     * @param optionType
     * @return
     */
    public static int askUserQuestion(Component parentCopmonent, Object question, String title, int optionType)
    {
        return JOptionPane.showConfirmDialog(parentCopmonent, question, title, optionType);
    }

    public static int askExitConfirm()
    {
        return JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_CANCEL_OPTION);
    }

    /**
     *
     * @param parentCopmonent
     * @param question
     * @param title
     * @param messageType
     */
    public static void showUserMessage(Component parentCopmonent, Object question, String title, int messageType)
    {
        JOptionPane.showMessageDialog(parentCopmonent, question, title, messageType);
    }

    //<editor-fold defaultstate="collapsed" desc="comment">
    /**
     * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and
     * time patterns interpreted in the U.S. locale">
     * <tr bgcolor="#ccccff">
     * <th align=left>Date and Time Pattern
     * <th align=left>Result
     * <tr>
     * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
     * <td><code>2001.07.04 AD at 12:08:56 PDT</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"EEE, MMM d, ''yy"</code>
     * <td><code>Wed, Jul 4, '01</code>
     * <tr>
     * <td><code>"h:mm a"</code>
     * <td><code>12:08 PM</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"hh 'o''clock' a, zzzz"</code>
     * <td><code>12 o'clock PM, Pacific Daylight Time</code>
     * <tr>
     * <td><code>"K:mm a, z"</code>
     * <td><code>0:08 PM, PDT</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
     * <td><code>02001.July.04 AD 12:08 PM</code>
     * <tr>
     * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
     * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyMMddHHmmssZ"</code>
     * <td><code>010704120856-0700</code>
     * <tr>
     * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
     * <td><code>2001-07-04T12:08:56.235-0700</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"</code>
     * <td><code>2001-07-04T12:08:56.235-07:00</code>
     * <tr>
     * <td><code>"YYYY-'W'ww-u"</code>
     * <td><code>2001-W27-3</code>
     * </table>
     *
     * @param date The date to be converted
     * @param formatFrom the format the date is in
     * @param formatTo the format to conver the date to
     * @param locale Locale object represents a specific geographical,
     * political, or cultural region. eg Locale.ENGLISH
     * @return the converted date
     * @throws ParseException
     */
    //</editor-fold>
    public static String dateFormatConverter(String date, String formatFrom, String formatTo, Locale locale) throws ParseException
    {
        DateFormat toFormat = new SimpleDateFormat(formatTo, locale);
        DateFormat fromFormat = new SimpleDateFormat(formatFrom, locale);
        return toFormat.format(fromFormat.parse(date));
    }

    //<editor-fold defaultstate="collapsed" desc="comment">
    /**
     * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and
     * time patterns interpreted in the U.S. locale">
     * <tr bgcolor="#ccccff">
     * <th align=left>Date and Time Pattern
     * <th align=left>Result
     * <tr>
     * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
     * <td><code>2001.07.04 AD at 12:08:56 PDT</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"EEE, MMM d, ''yy"</code>
     * <td><code>Wed, Jul 4, '01</code>
     * <tr>
     * <td><code>"h:mm a"</code>
     * <td><code>12:08 PM</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"hh 'o''clock' a, zzzz"</code>
     * <td><code>12 o'clock PM, Pacific Daylight Time</code>
     * <tr>
     * <td><code>"K:mm a, z"</code>
     * <td><code>0:08 PM, PDT</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
     * <td><code>02001.July.04 AD 12:08 PM</code>
     * <tr>
     * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
     * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyMMddHHmmssZ"</code>
     * <td><code>010704120856-0700</code>
     * <tr>
     * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
     * <td><code>2001-07-04T12:08:56.235-0700</code>
     * <tr bgcolor="#eeeeff">
     * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"</code>
     * <td><code>2001-07-04T12:08:56.235-07:00</code>
     * <tr>
     * <td><code>"YYYY-'W'ww-u"</code>
     * <td><code>2001-W27-3</code>
     * </table>
     *
     * @param dateToConvert The date to be converted
     * @param formatTo the format to conver the date to. If null format will be "dd/mm/yyyy"
     * @param locale Locale object represents a specific geographical,
     * political, or cultural region. eg Locale.ENGLISH if null Locale will be Locale.ENGLISH
     * @return the converted date
     */
    //</editor-fold>
    public static String toLocaleDate(Date dateToConvert, String formatTo, Locale locale)
    {
        if (formatTo == null)
        {
            formatTo = "dd/mm/yyyy";
        }
        if (locale == null)
        {
            locale = Locale.ENGLISH;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatTo, locale);
        dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
        //dateFormat.applyPattern(formatTo);

        return dateFormat.format(dateToConvert);
    }
}
